/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package GUI;

import Model.*;
import javax.microedition.lcdui.*;

/**
 *
 * @author Pisarchik_S
 */
public class ShowCategories  implements ItemCommandListener, CommandListener{

    Dispatcher m_Dispatcher;
    CModel m_Model;

    public Command [] m_Comands;

    public Command m_ShowMainMenu = new Command("Мэню", Command.CANCEL, 0);

    public ShowCategories(Dispatcher dispatcher, CModel model)
    {
        m_Dispatcher = dispatcher;
        m_Model = model;
    }

    public void Init()
    {
        Form form  = new Form("Рэдагаваць катэгорыі");
        int sz = m_Model.m_Categories.length;
        m_Comands = new Command[sz+1];
        Command cmd = new Command("Дадаць",Command.ITEM,0);
        StringItem si = new StringItem("","Дадаць карэгорыю");
        si.setDefaultCommand(cmd);
        si.setItemCommandListener(this);
        si.setPreferredSize(400, 10);
        m_Comands[0] = cmd;
        form.append(si);

        for (int i =0; i< sz; ++i)
        {
            String s = m_Model.m_Categories[i].GetName();

            cmd = new Command("Рэдагаваць",Command.ITEM,0);
            si = new StringItem("",s);
            si.setDefaultCommand(cmd);
            si.setItemCommandListener(this);
            si.setPreferredSize(400, 10);
            m_Comands[i+1] = cmd;
            form.append(si);
        }

        form.addCommand(m_ShowMainMenu);
        form.setCommandListener(this);

        m_Dispatcher.GetDisplay().setCurrent(form);
    }

    public void commandAction(Command c, Item item)
    {
        for (int i = 1; i < m_Comands.length; ++i)
        {
            if (c == m_Comands[i])
            {
                m_Dispatcher.GetEditCategory().Init(m_Model.m_Categories[i-1]);
            }
        }
        if (c == m_Comands[0])
        {
            m_Dispatcher.GetCreateNewCategory().Init();
        }
    }

     public void commandAction(Command c, Displayable s)
    {
        if (c == m_ShowMainMenu)
        {
            m_Dispatcher.GetMainMenuEditor().Init();
        }
    }
}
